package com.thx.workflow.custom.form;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.thx.common.spring.BeanFactory;
import com.thx.common.util.AppContext;
import com.thx.workflow.custom.model.OfficialDoc;
import com.thx.workflow.custom.service.OfficialDocService;
import com.topdt.jbpm.form.Document;
import com.topdt.jbpm.form.FormHandle;
import com.topdt.jbpm.form.FormUI;
import com.topdt.jbpm.model.Application;
import com.topdt.jbpm.model.Field;
import com.topdt.jbpm.model.Work;

/**
 * 政府公文处理
 * 建立日期 : 2015年12月25日 下午4:43:55<br>
 * 作者 : 冯晋首<br>
 * 模块 : <br>
 * 描述 : <br>
 * 修改历史: 序号 日期 修改人 修改原因 <br>
 * 1 <br>
 * 2 <br>
 */
public class OfficialDocHandle implements FormHandle{
	private static Logger logger = LoggerFactory.getLogger(OfficialDocHandle.class);
	
	@Override
	public FormUI assemble(Map<String, Object> context) {

		Work work = (Work) context.get("workObj");
		Document doc = this.getDocument(work.getWorkId());

		FormUI ui = new FormUI(doc, context);
		ui.setUiType(FormUI.UIType.url);
		ui.setUiURL("/customworkflow/officialDoc.jsp");
		return ui;
	}
	@Override
	public Document convert2Doc(Map<String, String[]> parameter) {
		OfficialDoc doc = new OfficialDoc();
		doc.setDocName(parameter.get("workTitle")[0]);
		doc.setEmergencyLevel(parameter.get("emergencyLevel")!=null?parameter.get("emergencyLevel")[0]:"");
		doc.setIsPublic(parameter.get("isPublic")!=null?parameter.get("isPublic")[0]:"");
		doc.setDocNum(parameter.get("docNum")!=null?parameter.get("docNum")[0]:"");
		doc.setPrintNum(parameter.get("printNum")!=null?Integer.valueOf(parameter.get("printNum")[0]):0);
		doc.setPublishDate(parameter.get("publishDate")!=null?parameter.get("publishDate")[0]:"");
		doc.setMainSend(parameter.get("mainSend")!=null?parameter.get("mainSend")[0]:"");
		doc.setCopySend(parameter.get("copySend")!=null?parameter.get("copySend")[0]:"");
		doc.setUnderTaker(parameter.get("underTaker")!=null?parameter.get("underTaker")[0]:"");
		doc.setContactWay(parameter.get("contactWay")!=null?parameter.get("contactWay")[0]:"");
		doc.setSignatureDate(parameter.get("signatureDate")!=null?parameter.get("signatureDate")[0]:"");
		doc.setDocYear(parameter.get("docYear")!=null?parameter.get("docYear")[0]:"");
		return doc;
	}

	@Override
	public Document getDocument(String documentId) {
		OfficialDocService service = (OfficialDocService) BeanFactory.getBean(OfficialDocService.class);
		List<OfficialDoc> list = service.findByWorkId(documentId);
		OfficialDoc doc = null;
		if(list.size() > 0) {
			doc = list.get(0);
		} else {
			doc = new OfficialDoc();
			doc.setDocId(documentId);
		}
		return doc;
	}

	@Override
	public void deleteDoc(String documentId) {
		logger.debug("<提示>:删除文档数据" + documentId);
	}

	@Override
	public void executeBusiness(Application app, Work work) {
		logger.debug("<提示>:执行业务逻辑");
	}

	@Override
	public Document saveForm(String workId, Document doc) {
		logger.debug("<提示>:保存表单数据");
		OfficialDoc publishDoc = (OfficialDoc) doc;
		OfficialDocService service = (OfficialDocService) BeanFactory.getBean(OfficialDocService.class);
		List<OfficialDoc> list = service.findByWorkId(workId);
		if (list.size() <= 0) {
			publishDoc.setWorkId(workId);
			publishDoc.setCompanyId(AppContext.getCurrentComId());
			service.save(publishDoc);
		} else {
			OfficialDoc old = list.get(0);
			old.setSignatureDate(publishDoc.getSignatureDate());
			old.setDocYear(publishDoc.getDocYear());
			service.save(old);
		}
		logger.debug("<提示>:表单数据=" + JSON.toJSONString(publishDoc));
		return publishDoc;
	}



	@Override
	public List<Field> getFields(Application app) {
		List<Field> fields=new ArrayList<Field>();
		
		Field field_docName=new Field();
		field_docName.setFieldName("docName");
		field_docName.setFieldText("文件名称");
		fields.add(field_docName);
		
		Field field_emergencyLevel=new Field();
		field_emergencyLevel.setFieldName("emergencyLevel");
		field_emergencyLevel.setFieldText("紧急程度");
		fields.add(field_emergencyLevel);
		
		Field field_isPublic=new Field();
		field_isPublic.setFieldName("isPublic");
		field_isPublic.setFieldText("是否公开");
		fields.add(field_isPublic);
		
		Field field_docNum=new Field();
		field_docNum.setFieldName("docNum");
		field_docNum.setFieldText("文号");
		fields.add(field_docNum);
		
		Field field_printNum=new Field();
		field_printNum.setFieldName("printNum");
		field_printNum.setFieldText("打印份数");
		fields.add(field_printNum);
		
		Field field_publishDate=new Field();
		field_publishDate.setFieldName("publishDate");
		field_publishDate.setFieldText("发文日期");
		fields.add(field_publishDate);
		
		Field field_mainSend=new Field();
		field_mainSend.setFieldName("mainSend");
		field_mainSend.setFieldText("主送");
		fields.add(field_mainSend);
		
		Field field_copySend=new Field();
		field_copySend.setFieldName("copySend");
		field_copySend.setFieldText("抄送");
		fields.add(field_copySend);
		
		Field field_underTaker=new Field();
		field_underTaker.setFieldName("underTaker");
		field_underTaker.setFieldText("负责人");
		fields.add(field_underTaker);
		
		return fields;
	}

}